<?php

namespace frontend\controllers;


use Yii;
use yii\web\Controller;
use yii\helpers\ArrayHelper;
use frontend\models\ArticleSearch;
use frontend\models\CategorySearch;
use yii\web\NotFoundHttpException;

/**
 * Site controller
 */
class SiteController extends Controller
{

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {

        $searchModel = new ArticleSearch();

        // 获取推荐产品
        $cid         = $this->findCids(18);
        $productList = $searchModel::find()->where(['article_category_id' => $cid, 'flag_recommend' => 1])->all();
        // 获取推荐新闻
        $cid      = $this->findCids(13);
        $newsList = $searchModel::find()->where(['article_category_id' => $cid, 'flag_recommend' => 1])->all();
        //头条跑马灯
        $cid          = $this->findCids(13);
        $headlineList = $searchModel::find()->where(['article_category_id' => $cid, 'flag_headline' => 1])->all();

//        $newsList=ArrayHelper::index($newsList);
        return $this->render(
            'index',
            [
                'productList'  => $productList,
                'newsList'     => $newsList,
                'headlineList' => $headlineList,
            ]
        );
    }

    /**
     * @Notes  列表访问
     * @Interface actionList
     *
     *
     * @return string
     * @throws NotFoundHttpException
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 17:29
     */
    public function actionList()
    {
        $view = yii::$app->getRequest()->get("view");
        if (empty($view)) {
            throw new NotFoundHttpException('The requested view is null .');
        }
        $id = yii::$app->getRequest()->get("id");
        if (empty($id)) {
            if ($view == 'news') {
                $id = !empty($id) ? $id : 13;//默认pcid18 产品分类
            }
            if ($view == 'product') {
                $id = !empty($id) ? $id : 18;//默认pcid18 产品分类
            }

        }

        return $this->render(
            $view,
            [
                'menus'    => $this->findMenus($id), //菜单
                'list'     => $this->findList($id),
                'category' => $this->findCategory($id),
            ]
        );
    }

    /**
     * @Notes  详情访问
     * @Interface actionView
     *
     *
     * @return string
     * @throws NotFoundHttpException
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 17:29
     */
    public function actionView()
    {
        $id   = yii::$app->getRequest()->get("id");
        $view = yii::$app->getRequest()->get("view");

        return $this->render(
            'view',
            [
                'model' => $this->findModel($id),
                'contr' => $view,
            ]
        );
    }


    /**
     * @Notes
     * @Interface findMenus
     *
     * @param string $id
     *
     * @return array
     * @throws NotFoundHttpException
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 16:16
     */
    private function findMenus($id)
    {
        if (empty($id)) {
            throw new NotFoundHttpException('The requested id is null .');
        }
        $_Category = $this->findCategory($id);
        $Categorys = $_Category->children()->asArray()->all();
        $Categorys = ArrayHelper::index($Categorys, 'id');
        $menus     = ArrayHelper::map($Categorys, 'id', 'name');

        return $menus;
    }

    /**
     * @Notes 获取列表数据
     * @Interface findList
     *
     * @param $id
     *
     * @return array|\common\models\Article[]
     * @throws NotFoundHttpException
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 19:29
     */
    private function findList($id)
    {
        $cids = $this->findCids($id);
        if (empty($cids)) {
            throw new NotFoundHttpException('The param cids is null .');
        }
        $searchModel = new ArticleSearch();
        $lists       = $searchModel::find()->where(['article_category_id' => $cids])->all();
        $lists       = ArrayHelper::index($lists, 'id');

        return $lists;
    }

    /**
     * @Notes  获取分类ids 方便查询
     * @Interface findCids
     *
     * @param $id
     *
     * @return array
     * @throws NotFoundHttpException
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 19:28
     */
    private function findCids($id)
    {
        $_Category = $this->findCategory($id);
        $Categorys = $_Category->children()->asArray()->all();
        $_Cids     = ArrayHelper::getColumn($Categorys, 'id', false);
        $_Cids[]   = $id;

        return $_Cids;
    }

    /**
     * @Notes   获取分类信息
     * @Interface findCategory
     *
     * @param $id
     *
     * @return CategorySearch|null
     * @throws NotFoundHttpException
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 19:28
     */
    private function findCategory($id)
    {
        if (empty($id)) {
            throw new NotFoundHttpException('The param id is empty .');
        }
        $searchModel = new CategorySearch();
        $Category    = $searchModel::findOne(['id' => $id]);

        return $Category;
    }

    /**
     * @Notes 获取详情信息
     * @Interface findModel
     *
     * @param string $id
     *
     * @return ArticleSearch|null
     * @throws NotFoundHttpException
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 19:28
     */
    private function findModel($id = '')
    {
        if (empty($id)) {
            $id = yii::$app->getRequest()->get("id");
        }
        if (($model = ArticleSearch::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The function findModel return null.');
        }
    }

    /**
     * @Notes  单页列表
     * @Interface actionAbout
     *
     *
     * @return string
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 12:59
     */
    public function actionAbout()
    {
        //获取分类下面所有文章
        $cid         = yii::$app->getRequest()->get("cid");
        $id          = yii::$app->getRequest()->get("id");
        $cid         = !empty($cid) ? $cid : 12;//默认id12 关于我们分类
        $id          = !empty($id) ? $id : 128;//默认id128公司简介
        $searchModel = new ArticleSearch();
        $Articles    = $searchModel->search(['article_category_id' => $cid])->getModels();
        $Articles    = ArrayHelper::index($Articles, 'id');
        $menus       = ArrayHelper::map($Articles, 'id', 'title');
        $Article     = $Articles[$id];

        return $this->render(
            'about',
            [
                'menus'   => $menus,
                'Article' => $Article,
            ]
        );
    }


    /**
     * @Notes  关于我们
     * @Interface actionContact
     *
     *
     * @return string
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 12:59
     */
    public function actionContact()
    {
        return $this->render('contact');
    }

    /**
     * @Notes 首页底部新闻轮播
     * @Interface actionIndexLayer
     *
     *
     * @return string
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 12:59
     */
    public function actionIndexLayer()
    {
        return $this->renderPartial('index-layer');
    }

    /**
     * @Notes  关于我们的地图引入页面
     * @Interface actionBaidu
     *
     *
     * @return string
     * @Author Aaron/刘志勇
     * @Email lzymail@foxmail.com
     * @Date 2020-01-08 13:00
     */
    public function actionBaidu()
    {
        return $this->renderPartial('baidu');
    }
}
